package com.zretc.controller;

import com.zretc.entity.Cart;
import com.zretc.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/cartController")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 查询购物车中的所有商品
     * @param userId
     * @return
     */
    @RequestMapping("/getCartAllList")
    @ResponseBody
    List<Cart> getCartAllList(Integer userId) {
        List<Cart> cartList = cartService.findCartAllList(userId);
        return cartList;
    }

    /**
     * 查询购物车中的商品
     * @param cartId
     * @return
     */
    @RequestMapping("/getCartAllBycartIdList")
    @ResponseBody
    Cart getCartAllBycartIdList(Integer cartId) {
        Cart cart = cartService.findCartAllBycartIdList(cartId);
        return cart;
    }

    /**
     * 删除购物车中的商品
     * @param cartId
     * @return
     */
    @RequestMapping("/deleteCartByCartId")
    @ResponseBody
    public boolean deleteCartByCartId(Integer cartId) {
        int n = cartService.deleteCartByCartId(cartId);
        if (n > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改商品数量
     * @param cartId 购物车Id
     * @param cartCount  数量
     * @return
     */
    @RequestMapping("/updateCartList")
    @ResponseBody
    public boolean updateCartList(Integer cartId, Integer cartCount) {
        int n = cartService.updateCartList(cartId, cartCount);
        if (n > 0) {
            return true;
        }
        return false;
    }

    /**
     * 添加到购物车
     * @param cart
     * @return
     */
    @RequestMapping("/insertCart")
    @ResponseBody
    public boolean insertCart(Cart cart) {
        int n = cartService.insertCart(cart);
        if (n > 0) {
            return true;
        }
        return false;
    }
}
